L1-003 个位数统计
题目 个位数统计
思路分析
有坑 直接用数字 %10 /10取每一位会过不了最后一个数据 用longlong也不行
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll n; cin >> n;
map<ll, int> cnt;
if(n==0){
cout<<"0:1"<<endl;
return 0;
}
while(n) {
int cur = n % 10;
cnt[cur]++;
n /= 10;
}
for (auto c : cnt) {
cout << c.first << ":" << c.second << endl;
}
return 0;
}
只能把输入作为字符串处理
代码实现
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
string s; cin>>s;
int cnt[10]={0};
for(char c:s){
int num = c - '0';
cnt[num]++;
}
for(int i=0;i<10;i++){
if(cnt[i]>0){
cout<<i<<":"<<cnt[i]<<endl;
}
}
return 0;
}
同类题型
视频讲解
⬅️ L1-002 打印沙漏 🏠 00-天梯赛 ➡️ L1-004 计算摄氏温度
💬 评论